package com.hiraeth.modules.monitor.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hiraeth.common.utils.PageParams;
import com.hiraeth.common.utils.R;
import com.hiraeth.modules.monitor.entity.MonitorAlertNotification;
import com.hiraeth.modules.monitor.service.MonitorAlertNotificationService;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@Api(tags = "告警渠道控制器")
@RestController
@RequestMapping("/monitor/notification")
public class MonitorAlertNotificationController {

    @Autowired
    private MonitorAlertNotificationService monitorAlertNotificationService;

    /**
     * 获取告警渠道分页列表
     *
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions("monitor:notification:page")
    public R getNotificationsListPage(@RequestParam Map<String, Object> params) {
        IPage<MonitorAlertNotification> page = monitorAlertNotificationService.queryPage(new PageParams(params));
        return R.ok().put("page", page);
    }

    @PostMapping("/add")
    @RequiresPermissions("monitor:notification:add")
    public R addMonitorAlertNotification(@RequestBody MonitorAlertNotification monitorAlertNotification) {
        monitorAlertNotificationService.save(monitorAlertNotification);
        return R.ok();
    }

    @PostMapping("/update")
    @RequiresPermissions("monitor:notification:update")
    public R updateMonitorAlertNotification(@RequestBody MonitorAlertNotification monitorAlertNotification) {
        monitorAlertNotificationService.updateById(monitorAlertNotification);
        return R.ok();
    }

    /**
     * 删除告警渠道
     */
    @RequestMapping("/delete")
    @RequiresPermissions("monitor:notification:delete")
    public R delete(@RequestBody Long[] ids){
        monitorAlertNotificationService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 根据渠道主键查询渠道信息
     */
    @RequestMapping("/{notificationId}")
    @RequiresPermissions("monitor:notification:info")
    public R getNotificationById(@PathVariable("notificationId") Long notificationId){
        MonitorAlertNotification notification = monitorAlertNotificationService.getById(notificationId);
        return R.ok().put("notification", notification);
    }
}
